package com.pet.petadoption.user.controller;

import com.pet.petadoption.user.biz.imp.UserBizImpl;
import com.pet.petadoption.user.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("user")
@Log4j2
@CrossOrigin(origins = "http://localhost:8080",maxAge = 36000)
public class UserController {
    @Autowired
    UserBizImpl userBiz;

    @RequestMapping("/login")
    @ResponseBody
    User login(String account, String upwd, HttpSession session) {

        User user = userBiz.login(account, upwd);
        if(user == null){
            return user;
        }
        log.warn("user======login" + user + account + "   " + upwd);
        String username = user.getUname();
        //将用户名存入session作用域中
        session.setAttribute("username", username);
        return user;
    }


    @RequestMapping("/getAllUser")
    @ResponseBody
    Map<String, Object> getAllUser(Integer index,Integer status) {
        Integer newIndex = index;
        if (newIndex == null) {
            newIndex = 1;
        }
        int size = 5;
        List<User> list = userBiz.findUsers(newIndex, size,status);
        System.out.println("查询数据库 list<User>=====>" + list.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("list", list);
        return map;
    }


    //上传
    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException, IOException {
        String fileName = "";
        if (!file.isEmpty()) {
            //获取文件名
            fileName = file.getOriginalFilename();
            //定义上传的路径：
            String filePath = "D:\\大三下册实训\\petadoption\\petadoption_web\\src\\assets\\img\\";
            //将图片存入imag文件夹下
            File file1 = new File(filePath, fileName);
            //将文件写入磁盘
            file.transferTo(file1);
            System.out.println("upload=============>" + fileName);
        }
        return fileName;
    }

    @RequestMapping(value = "/register")
    @ResponseBody
    public String register(String uname, String upwd, String phone,
                           String email, String avatar, String addr, String sex, String age) {
        User user = new User();
        //获取用户账号
        int count = userBiz.findUsers(1, Integer.MAX_VALUE,0).size();
        String account = Integer.toString(count);
        Random random = new Random();
        int h = random.nextInt(100);
        account += Integer.toString(h);
        user.setAccount(account);

        user.setUpwd(upwd);
        user.setAddr(addr);
        user.setAvatar(avatar);
        user.setEmail(email);
        user.setPhone(phone);
        user.setUname(uname);
        user.setSex(sex);
        user.setAge(Integer.valueOf(age));
        log.warn("register =>" + user.toString());
        if (userBiz.addUser(user)) {
            System.out.println("return" + account);
            return account;
        } else {
            return null;
        }

    }


    @RequestMapping(value = "/findUserById")
    @ResponseBody
    public List<User> findUserById(Integer id,Integer status) {
        User user = userBiz.findUserById(id,status);
        log.warn("findUserById"+user.toString());
        List<User> list = new ArrayList<User>();

        if(user==null){
            return list;
        }
        list.add(user);

        return list;
    }

    @RequestMapping(value = "/findUserByUname")
    @ResponseBody
    public List<User> findUserByUname(String info,Integer status) {

        return userBiz.findUserByUname(info,status);
    }


    @RequestMapping(value = "/changeUserStatus")
    @ResponseBody
    public boolean changeUserStatus(Integer id,Integer  status) {
        System.out.println(id+"  "+status);
        Integer row = userBiz.changeUserStatus(id,status);
        log.warn(row);
        return row==1;
    }



    @RequestMapping(value = "/delArr")
    @ResponseBody
    public boolean delArr(String ids) {
        Integer row = userBiz.delArr(ids);
        log.warn(row);
        return row>0;
    }

    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public Boolean updateUser(String id,String uname,String upwd,String age,String phone,String email,String avatar){
        User user= new User();
        user.setId(Integer.valueOf(id));
        user.setAge(Integer.valueOf(age));
        user.setUname(uname);
        user.setUpwd(upwd);
        user.setPhone(phone);
        user.setEmail(email);
        user.setAvatar(avatar);
        log.warn(user);
        if(userBiz.updateUser(user) == 1){
            return true;
        }
        return false;
    }


}